const serviceConfig = (function($){
    $.servicePath = 'http://localhost:8888/'
    $.serviceUserPath = $.servicePath + 'user/'
    $.serviceBookPath = $.servicePath + 'book/'

    $.loginUrl = $.serviceUserPath + 'login'
    $.registerUrl = $.serviceUserPath + 'register'
    $.findAllBookUrl = $.serviceBookPath + 'findAll'
    $.addBookUrl = $.serviceBookPath + 'addBook'
    $.findPageBookUrl = $.serviceBookPath + 'findPage'

    $.codeUrl = $.servicePath + 'image/createImage'

    return $;
})(window.serviceConfig||{});
var webConfig = (function ($) {
    $.webPath = 'http://localhost:63342/library/templates/'
    $.userDirPath = $.webPath + 'user/'
    $.bookDirPath = $.webPath + 'book/'

    $.loginHtml = $.userDirPath + 'login.html';
    $.registerHtml = $.userDirPath + 'register.html'
    $.booksHtml = $.bookDirPath + 'books.html'
    $.addBookHtml = $.bookDirPath + 'addBook.html'

    return $;
})(window.webConfig||{});
var methodConfig = (function (m) {
    m.ajaxMethod = function(type,url,async,data){
        return new Promise(function (resolve, reject) {

            var promise = $.ajax({
                type: type,
                dataType: "json",
                url: url ,
                async: async,
                xhrFields: {withCredentials: true},
                crossDomain: true,
                data: data
            })
            promise.then(function (request) {
                resolve(request)
            }).catch(function (error) {
                reject(error)
            })
        })
    }
    m.requestMethod = function (request) {
        switch (request.status) {
            case 0:
                return true
            case 1:
                alert(request.msg)
                window.location.href=webConfig.loginHtml
        }
    }

    return m;
})(window.webConfig||{});
var apiConfig = (function ($) {
    $.loginApi = ({username,password,code}) => (methodConfig.ajaxMethod('POST',serviceConfig.loginUrl,false,{username,password,code}))
    $.registerApi = ({username,password}) => (methodConfig.ajaxMethod('POST',serviceConfig.registerUrl,false,{username,password}))
    $.findAllBookApi = () => (methodConfig.ajaxMethod('POST',serviceConfig.findAllBookUrl,false,{}))
    $.addBookApi = ({name,author,price}) => (methodConfig.ajaxMethod('POST',serviceConfig.addBookUrl,false,{name,author,price}))
    $.findPageBookApi = ({pageNum,pageSize}) => (methodConfig.ajaxMethod('POST',serviceConfig.findPageBookUrl,false,{pageNum,pageSize}))

    return $;
})(window.webConfig||{});